using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Managistics.Warehouse.Configuration;
using Managistics.Warehouse.Controllers.Interfaces;
using Managistics.Warehouse.Views.Interfaces;
using Managistics.Warehouse.Factories;
using Managistics.Core.Services;
using Managistics.Model;
using log4net;

namespace Managistics.Warehouse.Controllers
{
    public class MainController : IMainController
    {
        /// <summary>
        /// Local instance of the configuration class that we'll end up binding to
        /// </summary>
        public ApplicationConfiguration appConfig = null;

        private readonly log4net.ILog Logger;

        private readonly IBasicService basicService;
        private readonly IGoodService goodService;
        private readonly ISaleOrderService saleOrderService;

        readonly IMainView mainView;
        readonly IControllerFactory controllerFactory;

        public MainController(IMainView mainView, IControllerFactory controllerFactory, ILoadingController loadingController,
            IBasicService basicService, IGoodService goodService, ISaleOrderService saleOrderService)
        {
            GlobalApp.LoadingScreen = loadingController;
            //GlobalApp.LoadingScreen.Run();

            this.Logger = GlobalApp.Logger;
            this.mainView = mainView;
            this.controllerFactory = controllerFactory;
            this.basicService = basicService;
            this.goodService = goodService;
            this.saleOrderService = saleOrderService;

            mainView.ShowSubFormClicked += ShowSubForm;
            mainView.ShowDashoardFormClicked += ShowDashoardForm;

            // Purchase
            mainView.ShowViewEnterFromPurchaseFormClicked += ShowViewEnterFromPurchaseForm;
            mainView.ShowPurchaseReportsFormClicked += ShowPurchaseReportsForm;
            mainView.ShowViewEnterInputSheetFormClicked += ShowViewEnterInputSheetForm;

            // Inventory
            mainView.ShowAdjustParityRetailCodeFormClicked += ShowAdjustParityRetailCodeForm;
            mainView.ShowCalculateTotalInOutputFormClicked += ShowCalculateTotalInOutputForm;
            // Sale
            mainView.ShowSaleReportsFormClicked += ShowSaleReportsForm;
            mainView.ShowViewEnterOutputFromSaleFormClicked += ShowViewEnterOutputFromSaleForm;
            mainView.ShowViewEnterOutputSheetFormClicked += ShowViewEnterOutputSheetForm;

            // show splash screen for loading data
            // Simply assign the default config object - it gets loaded via
            // the default constructor defined in AppConfig.cs
            this.appConfig = GlobalApp.Configuration;

            // get all basic data for entire application
            InitializeGlobalData();

            // force to re-read in case we updated previously
            appConfig.Read();

            //GlobalApp.LoadingScreen.Stop(GlobalApp.ThreadOfWaiting);
        }

        protected void InitializeGlobalData() {
            // get all goods
            //GlobalApp.AllGoods = goodService.GetGoodList();
            //// get all suppliers, customers
            //GlobalApp.AllSuppliers = basicService.GetAllSuppliers();

            //GlobalApp.AllCustomers = basicService.GetAllCustomers();

            //GlobalApp.AllEmployees = basicService.GetAllEmployees();

            //Examples
            //SaleOrder saleOrder = saleOrderService.GetOrderById("ORC0000074462");
        }

        public void Run()
        {
            // show main form
            mainView.Open();
        }

        public void Stop(string key) { }

        void ShowSubForm()
        {
            var parameter = new ComplexParameterObject {Text = "parameter from main"};
            var subController = controllerFactory.CreateSubController(parameter);

            subController.Run();

            // call in case show form with modal dialog
            // execute when the form closed
            controllerFactory.ReleaseController(subController);
        }

        void ShowDashoardForm()
        {
            var parameter = new ComplexParameterObject { Text = "parameter from main" };
            var dashboardController = controllerFactory.CreateDashboardController(this.mainView, parameter);

            // show this form inside the parent form
            dashboardController.Run();
        }

        void ShowViewEnterFromPurchaseForm()
        {
            var purchaseController = controllerFactory.CreatePurchaseController(this.mainView);

            // show this form inside the parent form
            purchaseController.Run();
        }

        void ShowAdjustParityRetailCodeForm()
        {
            var adjustController = controllerFactory.CreateInventoryAdjustController();
            adjustController.Run();
            controllerFactory.ReleaseController(adjustController);
        }

        void ShowCalculateTotalInOutputForm()
        {
            var calculateTotalController = controllerFactory.CreateInventoryCalculateTotalController();
            calculateTotalController.Run();
            controllerFactory.ReleaseController(calculateTotalController);
        }

        void ShowSaleReportsForm()
        {
            var saleReportsController = controllerFactory.CreateSaleReportsController();
            saleReportsController.Run();
            controllerFactory.ReleaseController(saleReportsController);
        }

        void ShowViewEnterOutputFromSaleForm()
        {
            var viewEnterOutputController = controllerFactory.CreateViewEnterOutputFromSaleController();
            viewEnterOutputController.Run();
            controllerFactory.ReleaseController(viewEnterOutputController);
        }

        void ShowViewEnterOutputSheetForm()
        {
            var viewEnterOutputController = controllerFactory.CreateViewEnterOutputSheetController();
            viewEnterOutputController.Run();
            controllerFactory.ReleaseController(viewEnterOutputController);
        }

        void ShowPurchaseReportsForm()
        {
            var purchaseReportsController = controllerFactory.CreatePurchaseReportsController();
            purchaseReportsController.Run();
            controllerFactory.ReleaseController(purchaseReportsController);
        }

        void ShowViewEnterInputSheetForm()
        {
            var viewEnterInputController = controllerFactory.CreateViewEnterInputSheetController();
            viewEnterInputController.Run();
            controllerFactory.ReleaseController(viewEnterInputController);
        }
    }
}